//
//  SACommodityAddShopSizeView.m
//  yidiantong
//
//  Created by 黄志刚 on 2020/11/20.
//

#import "SACommodityAddShopSizeView.h"
#import "SAInventoryCollectionViewCell.h"
#import "SAProductSizeORColorModel.h"
#import "SACommodityAddShopSizeCell.h"
#import "SAProductChooseColorModel.h"
#import "SASalesOrderViewListModel.h"

@interface SACommodityAddShopSizeView ()<UITableViewDataSource, UITableViewDelegate,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UITextFieldDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIView *headerView;
@property (nonatomic, strong) UIButton *switchBtn;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UIView *footerView;
@property (nonatomic, strong) NSMutableArray *sizeMArr;
@property (nonatomic, assign) NSInteger current;
@property (nonatomic, assign) CGFloat collectionViewHeight;
@property (nonatomic, assign) BOOL isMore;//开启多选
@property (nonatomic, assign) NSInteger selectNum;//计算选中的数量
@property (nonatomic, strong) UIImageView *headerImageV;
@property (nonatomic, strong) SASalesOrderViewListModel *listMode;
@property (nonatomic, strong) NSMutableArray<SASalesOrderColoursList *> *selectColourMarr;
//记录初始数据
@property (nonatomic, strong) NSMutableArray<SASalesOrderToPendSkuList *> *oldSkuListMarr;
@property (nonatomic, strong) NSMutableArray<SASalesOrderColoursList *>*oldColourMarr;
@property (nonatomic, strong) UILabel *sizeLab;
@property (nonatomic, strong) UILabel *inventoryLab;
@property (nonatomic, strong) UIButton *cutBtn;
@property (nonatomic, strong) UIButton *addBtn;
@property (nonatomic, strong) UITextField *numTf;
@property (nonatomic, strong) UIButton *cancelBtn;
@property (nonatomic, strong) UIButton *giveBtn;

//开启赠送需要传入
@property (nonatomic, strong) NSMutableArray<SASalesOrderViewListModel *> *giveDataMarr;

@end

@implementation SACommodityAddShopSizeView

-(instancetype)initWithListModel:(SASalesOrderViewListModel *)listMode {
    if (self = [super init]) {
        self.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
        self.listMode = listMode;
        
        //记录开始的数量，如果用户取消就还原
        NSMutableArray<SASalesOrderToPendSkuList *> *oldSkuListMarr = [NSMutableArray array];
        for (int i =0 ; i < listMode.toPendSkuAttrs.count; i++) {
            SASalesOrderToPendSkuList *newModel = [SASalesOrderToPendSkuList new];
            newModel.stock = listMode.toPendSkuAttrs[i].stock;
            [oldSkuListMarr addObject:newModel];
        }
        self.oldSkuListMarr = oldSkuListMarr;
        
        if (listMode.colours.count > 0) {
            NSMutableArray<SASalesOrderColoursList *> *oldColourMarr = [NSMutableArray array];
            self.oldColourMarr = oldColourMarr;
            [self.selectColourMarr addObject:listMode.colours[0]];
            for (int i =0 ; i < listMode.colours.count; i++) {
                SASalesOrderColoursList *newList = [SASalesOrderColoursList new];
                newList.num = listMode.colours[i].num;
                [self.oldColourMarr addObject:newList];
                listMode.colours[i].isSelecd = NO;
            }
            listMode.colours[0].isSelecd = YES;
        }
        
        NSMutableArray *sizeMArr = [NSMutableArray array];
        for (int i = 0; i < listMode.colours.count; i++) {
            NSMutableArray<SASalesOrderToPendSkuList *> *newMarr = [NSMutableArray array];
            for (SASalesOrderToPendSkuList *model in listMode.toPendSkuAttrs) {
                if ([listMode.colours[i].attrName isEqualToString:model.colourName]) {
                    [newMarr addObject:model];
                }
            }
            [sizeMArr addObject:newMarr];
        }
        self.sizeMArr = sizeMArr;
        
        //不能移动位置
        self.isMore = NO;
        self.current = 0;
        [self creatUI];
    }
    return self;
}

#pragma mark - UICollectionView Delegate & DataSource
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.listMode.colours.count;
}

-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"SAInventoryCollectionViewCell";
    SAInventoryCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    cell.coloursModel = self.listMode.colours[indexPath.row];
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    SASalesOrderColoursList *model = self.listMode.colours[indexPath.row];
    if (self.isMore == YES) { //是否多选
        model.isSelecd = !model.isSelecd;
        self.current = indexPath.row;
        if (model.isSelecd == YES) {
            [self.selectColourMarr addObject:model];
        }else {
            [self.selectColourMarr removeObject:model];
        }
        if (self.selectColourMarr.count == 0) {
            self.current = -1;
        }else {
            self.current = [self.listMode.colours indexOfObject:self.selectColourMarr[0]];
        }
        [self.tableView reloadData];
        [self.collectionView reloadData];
    }else {
        if (self.current == -1) {
            model.isSelecd = YES;
            self.current = indexPath.row;
        }else {
            model.isSelecd = !model.isSelecd;
            if (model.isSelecd == YES) {
                self.listMode.colours[self.current].isSelecd = NO;
                self.current = indexPath.row;
            }else {
                self.current = -1;
            }
        }
        [self.collectionView reloadData];
        [self.tableView reloadData];
    }
}

#pragma mark - UITableView Delegate & DataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.current == -1) {
        return 0;
    }
    NSMutableArray *marr = self.sizeMArr[self.current];
    return marr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SACommodityAddShopSizeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SACommodityAddShopSizeCell" forIndexPath:indexPath];
    cell.isWarehousingStock = self.isWarehousingStock;
    cell.isNegative = self.isNegative;
    NSMutableArray *marr = self.sizeMArr[self.current];
    SASalesOrderToPendSkuList *model = marr[indexPath.row];
    
    cell.skuModel = model;
    __weak typeof(self) weakSelf = self;
    cell.inputBlock = ^(NSString * _Nonnull str) {
        if (weakSelf.isMore == YES) {
            for (int i = 0; i < weakSelf.selectColourMarr.count; i++) {
                NSInteger index2 = [weakSelf.listMode.colours indexOfObject:weakSelf.selectColourMarr[i]];
                NSMutableArray *marr2 = weakSelf.sizeMArr[index2];
                SASalesOrderToPendSkuList *model2 = marr2[indexPath.row];
                if (self.isNegative == YES) {
                    if ([str integerValue] > [model2.inventoryCount integerValue]) {
                        if ([model2.inventoryCount integerValue] >= 0) {
                            model2.stock = model2.inventoryCount;
                        }else {
                            model2.stock = @"0";
                        }
                    }else {
                        model2.stock = str;
                    }
                }else {
                    model2.stock = str;
                }
            }
        }else {
            if (self.isNegative == YES) {
                if ([str integerValue] > [model.inventoryCount integerValue]) {
                    if ([model.inventoryCount integerValue] >= 0) {
                        model.stock = model.inventoryCount;
                    }else {
                        model.stock = @"0";
                    }
                    [Prompt popPromptViewWithMsg:[NSString stringWithFormat:@"库存不足,当前库存%@件",model.inventoryCount] duration:2];
                }else {
                    model.stock = str;
                }
            }else {
                model.stock = str;
            }
        }
        [weakSelf combinedNum];
    };
    
    cell.addBlock = ^{
        if (weakSelf.isMore == YES) {
            for (int i = 0; i < weakSelf.selectColourMarr.count; i++) {
                NSInteger index2 = [weakSelf.listMode.colours indexOfObject:weakSelf.selectColourMarr[i]];
                NSMutableArray *marr2 = weakSelf.sizeMArr[index2];
                SASalesOrderToPendSkuList *model2 = marr2[indexPath.row];
                NSInteger num = [model2.stock integerValue];
                if (self.isNegative == YES) {
                    if ([model2.stock integerValue] + 1 > [model2.inventoryCount integerValue]) {
                        if ([model2.inventoryCount integerValue] >= 0) {
                            model2.stock = model2.inventoryCount;
                        }else {
                            model2.stock = @"0";
                        }
                    }else {
                        model2.stock = [NSString stringWithFormat:@"%zd",num + 1];
                    }
                }else {
                    model2.stock = [NSString stringWithFormat:@"%zd",num + 1];
                }
                
            }
        }else {
            if (self.isNegative == YES) {
                if ([model.stock integerValue] + 1 > [model.inventoryCount integerValue]) {
                    if ([model.inventoryCount integerValue] >= 0) {
                        model.stock = model.inventoryCount;
                    }else {
                        model.stock = @"0";
                    }
                    [Prompt popPromptViewWithMsg:[NSString stringWithFormat:@"库存不足,当前库存%@件",model.inventoryCount] duration:2];
                }else {
                    model.stock = [NSString stringWithFormat:@"%zd",[model.stock integerValue] + 1];
                }
            }else {
                model.stock = [NSString stringWithFormat:@"%zd",[model.stock integerValue] + 1];
            }
        }
        [weakSelf combinedNum];
    };
    
    cell.cutBlock = ^{
        if (weakSelf.isMore == YES) {
            for (int i = 0; i < weakSelf.selectColourMarr.count; i++) {
                NSInteger index2 = [weakSelf.listMode.colours indexOfObject:weakSelf.selectColourMarr[i]];
                NSMutableArray *marr2 = weakSelf.sizeMArr[index2];
                SASalesOrderToPendSkuList *model2 = marr2[indexPath.row];
                NSInteger num = [model2.stock integerValue];
                if (self.isWarehousingStock == YES) {
                    model2.stock = [NSString stringWithFormat:@"%zd",num - 1];
                }else {
                    if (num > 0) {
                        model2.stock = [NSString stringWithFormat:@"%zd",num - 1];
                    }else{
                        model2.stock = @"0";
                    }
                }
            }
        }else {
            if (self.isWarehousingStock == YES) {
                model.stock = [NSString stringWithFormat:@"%zd",[model.stock integerValue] - 1];
            }else {
                if ([model.stock integerValue] > 0) {
                    model.stock = [NSString stringWithFormat:@"%zd",[model.stock integerValue] - 1];
                }else {
                    model.stock = @"0";
                }
            }
        }
        [weakSelf combinedNum];
    };
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return kScaleNum(40);
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc] initWithFrame:CGRectZero];
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] initWithFrame:CGRectZero];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return kScaleNum(0.01);
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return kScaleNum(0.01);;
}


- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if (string.length == 0) {
        return YES;
    }
    if (textField.text.length > 4){
        return NO;
    }
    if ([string isEqualToString:@"-"] && textField.text.length > 0) {
        return NO;
    }
    return [self validateNumber:string];
}

#pragma mark - UITextField
- (void)textFieldEditChanged:(UITextField *)textField {
    NSString *num =  [NSString stringWithFormat:@"%zd",[textField.text integerValue]];
    
    NSMutableArray<SASalesOrderColoursList *> *selectMarr = [NSMutableArray array];
    for (SASalesOrderColoursList *model in self.listMode.colours) {
        if (model.isSelecd == YES) {
            [selectMarr addObject:model];
        }
    }
    
    for (int i = 0; i < selectMarr.count; i++) {
        NSInteger index2 = [self.listMode.colours indexOfObject:selectMarr[i]];
        NSMutableArray *marr2 = self.sizeMArr[index2];
        for (SASalesOrderToPendSkuList *model2 in marr2) {
            model2.stock = num;
            if (self.isWarehousingStock != YES) {
                if ([model2.stock doubleValue] < 0) {
                    model2.stock = @"0";
                }else {
                    if (self.isNegative == YES && [model2.stock integerValue] > [model2.inventoryCount integerValue]) {
                        if ([model2.inventoryCount integerValue] >= 0) {
                            model2.stock = model2.inventoryCount;
                        }else {
                            model2.stock = @"0";
                        }
                    }
                }
            }
        }
    }
    [self.tableView reloadData];
    [self combinedNum];
}

- (BOOL)validateNumber:(NSString*)number {
    BOOL res = YES;
    NSCharacterSet* tmpSet = [NSCharacterSet characterSetWithCharactersInString:@"-0123456789"];
    int i = 0;
    while (i < number.length) {
        NSString * string = [number substringWithRange:NSMakeRange(i, 1)];
        NSRange range = [string rangeOfCharacterFromSet:tmpSet];
        if (range.length == 0) {
            res = NO;
            break;
        }
        i++;
    }
    return res;
}

#pragma mark - event
-(void)clickedSwitchBtn:(UIButton *)sender {
    sender.selected = !sender.isSelected;
    if (sender.selected == YES) {
        self.isMore = YES;
        [self.selectColourMarr removeAllObjects];
        if (self.current != -1) {
            [self.selectColourMarr addObject:self.listMode.colours[self.current]];
        }
    }else{
        self.isMore = NO;
        if (self.selectColourMarr.count == 0) {
            self.current = -1;
        }else {
            NSInteger index = [self.listMode.colours indexOfObject:self.selectColourMarr[0]];
            self.current = index;
            for (SASalesOrderColoursList *model in self.listMode.colours) {
                model.isSelecd = NO;
            }
            self.listMode.colours[self.current].isSelecd = YES;
        }
        [self.collectionView reloadData];
        [self.tableView reloadData];
    }
}

- (void)clickedConfirmBtn {
    
    if (self.giveBtn.isHidden == NO) {
        int count = 0;
        for (SASalesOrderViewListModel *giveModel in self.giveDataMarr) {
            if ([giveModel.productId isEqualToString:self.listMode.productId]) {
                count++;
            }
        }
        if (count > 1) {
            if (self.listMode.isGive == YES) {
                [Prompt popPromptViewWithMsg:@"已存在相同的非赠送商品" duration:2];
                return;
            }
        }
    }
    
    if (self.isAll == YES) {
        self.listMode.isGive = NO;
        !self.selectBlock ?: self.selectBlock(self.listMode);
    }else {
        NSMutableArray<SASalesOrderToPendSkuList *> *marr = [NSMutableArray array];
        for (int i = 0; i < self.listMode.colours.count; i++) {
            for (SASalesOrderToPendSkuList *model in self.sizeMArr[i]) {
                if ([model.stock integerValue] != 0) {
                    [marr addObject:model];
                }
            }
        }
        self.listMode.toPendSkuAttrs = marr;
        self.listMode.isGive = NO;
        !self.selectBlock ?: self.selectBlock(self.listMode);
    }
    
    [UIView animateWithDuration:0.5f animations:^{
        self.alpha = 0.0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

-(void)combinedNum {
    for (int i = 0; i < self.listMode.colours.count; i++) {
        NSMutableArray *marr = self.sizeMArr[i];
        NSInteger num = 0;
        for (SASalesOrderToPendSkuList *model in marr) {
            num += [model.stock integerValue];
        }
        self.listMode.colours[i].num = @(num).stringValue;
    }
    [self.collectionView reloadData];
}

-(void)clickedImageV {
    !self.showImageBlock ?: self.showImageBlock();
}

-(void)clickedCutBtn {
    NSInteger num = [self.numTf.text integerValue];
    self.numTf.text = [NSString stringWithFormat:@"%zd",num - 1];
    
    NSMutableArray<SASalesOrderColoursList *> *selectMarr = [NSMutableArray array];
    for (SASalesOrderColoursList *model in self.listMode.colours) {
        if (model.isSelecd == YES) {
            [selectMarr addObject:model];
        }
    }
    
    for (int i = 0; i < selectMarr.count; i++) {
        NSInteger index2 = [self.listMode.colours indexOfObject:selectMarr[i]];
        NSMutableArray *marr2 = self.sizeMArr[index2];
        for (SASalesOrderToPendSkuList *model2 in marr2) {
            NSInteger num2 = [model2.stock integerValue];
            model2.stock = [NSString stringWithFormat:@"%zd",num2 - 1];
            if (self.isWarehousingStock != YES) {
                if ([model2.stock doubleValue] < 0) {
                    model2.stock = @"0";
                }else {
                    if (self.isNegative == YES && [model2.stock integerValue] > [model2.inventoryCount integerValue]) {
                        if ([model2.inventoryCount integerValue] >= 0) {
                            model2.stock = model2.inventoryCount;
                        }else {
                            model2.stock = @"0";
                        }
                    }
                }
            }
        }
    }
    [self.tableView reloadData];
    [self combinedNum];
}

-(void)clickedAddBtn {
    NSInteger num = [self.numTf.text integerValue];
    self.numTf.text = [NSString stringWithFormat:@"%zd",num + 1];
    
    NSMutableArray<SASalesOrderColoursList *> *selectMarr = [NSMutableArray array];
    for (SASalesOrderColoursList *model in self.listMode.colours) {
        if (model.isSelecd == YES) {
            [selectMarr addObject:model];
        }
    }
    
    for (int i = 0; i < selectMarr.count; i++) {
        NSInteger index2 = [self.listMode.colours indexOfObject:selectMarr[i]];
        NSMutableArray *marr2 = self.sizeMArr[index2];
        for (SASalesOrderToPendSkuList *model2 in marr2) {
            NSInteger num2 = [model2.stock integerValue];
            model2.stock = [NSString stringWithFormat:@"%zd",num2 + 1];
        }
    }
    [self.tableView reloadData];
    [self combinedNum];
}

//显示赠送
-(void)showGive:(NSMutableArray<SASalesOrderViewListModel *> *)giveDataMarr {
    self.giveDataMarr = giveDataMarr;
    self.cancelBtn.hidden = NO;
    self.giveBtn.hidden = NO;
}

//赠送事件
- (void)clickedGiveBtn {
    
    int count = 0;
    for (SASalesOrderViewListModel *giveModel in self.giveDataMarr) {
        if ([giveModel.productId isEqualToString:self.listMode.productId]) {
            count++;
        }
    }
    if (count > 1) {
        if (self.listMode.isGive != YES) {
            [Prompt popPromptViewWithMsg:@"已存在相同的赠送商品" duration:2];
            return;
        }
    }
    
    if (self.isAll == YES) {
        self.listMode.isGive = YES;
        !self.selectBlock ?: self.selectBlock(self.listMode);
    }else {
        NSMutableArray<SASalesOrderToPendSkuList *> *marr = [NSMutableArray array];
        for (int i = 0; i < self.listMode.colours.count; i++) {
            for (SASalesOrderToPendSkuList *model in self.sizeMArr[i]) {
                if ([model.stock integerValue] != 0) {
                    [marr addObject:model];
                }
            }
        }
        self.listMode.toPendSkuAttrs = marr;
        self.listMode.isGive = YES;
        !self.selectBlock ?: self.selectBlock(self.listMode);
    }
    
    [UIView animateWithDuration:0.5f animations:^{
        self.alpha = 0.0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

#pragma mark - creatUI
-(void)creatUI {
    
    self.userInteractionEnabled = YES;
    self.backgroundColor = kColor(0, 0, 0, 0.6);
    NSInteger count = 0;
    if (self.listMode.colours.count > 4) {
        count = ceilf(self.listMode.colours.count / 4.0);
    }else {
        count = 1;
    }
    
    if (count > 3){
        count = 3;
        self.collectionViewHeight = count * 32 + (count - 1) * 10 + 30;
    }else {
        self.collectionViewHeight = count * 32 + (count - 1) * 10 + 15;
    }
    
    UIButton *deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    deleteBtn.frame = CGRectMake(0, 0, kScreenWidth, kScaleNum(166));
    [deleteBtn addTarget:self action:@selector(shareDissTapEvent) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:deleteBtn];
    
    //    [self addSubview:self.headerView];
    [self addSubview:self.footerView];
    [self.headerView addSubview:self.switchBtn];
    [self.headerView addSubview:self.collectionView];
    [self.headerView addSubview:self.sizeLab];
    [self.headerView addSubview:self.inventoryLab];
    [self.headerView addSubview:self.cutBtn];
    [self.headerView addSubview:self.addBtn];
    [self.headerView addSubview:self.numTf];
    [self addSubview:self.tableView];
    self.tableView.tableHeaderView = self.headerView;
    [self addSubview:self.headerImageV];
    
    self.switchBtn.selected = self.isMore;
    [self combinedNum];//计算数量
    self.numTf.text = @"0";
}

-(void)shareDissTapEvent {
    for (int i =0 ; i < self.listMode.toPendSkuAttrs.count; i++) {
        self.listMode.toPendSkuAttrs[i].stock = self.oldSkuListMarr[i].stock;
    }
    for (int i =0 ; i < self.listMode.colours.count; i++) {
        self.listMode.colours[i].num = self.oldColourMarr[i].num;
    }
    
    [UIView animateWithDuration:0.5f animations:^{
        self.alpha = 0.0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (void)showView:(UIViewController *)Sview {
    if(Sview == nil){
        [[UIApplication sharedApplication].keyWindow addSubview:self];
    }else{
        [Sview.view addSubview:self];
    }
}

#pragma mark - lazy
-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kScaleNum(166), kScreenWidth, kScreenHeight - kScaleNum(166 + SafeFooterHeight + 40)) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = kBgColor;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        [_tableView registerClass:[SACommodityAddShopSizeCell class] forCellReuseIdentifier:@"SACommodityAddShopSizeCell"];
    }
    return _tableView;
}

-(UIView *)headerView {
    if (!_headerView) {
        //        _headerView = [[UIView alloc] initWithFrame:SARectMake(0, 166, kBaseScreenWidth, (130 + self.collectionViewHeight))];
        _headerView = [[UIView alloc] initWithFrame:SARectMake(0, 0, kBaseScreenWidth, (130 + self.collectionViewHeight))];
        _headerView.backgroundColor = [UIColor whiteColor];
        UILabel *titleLab = [[UILabel alloc] initWithFrame:SARectMake(90, 6, 200, 15)];
        titleLab.text = self.listMode.productCode;
        titleLab.textColor = kBlackColor;
        titleLab.font = kFont(13);
        [_headerView addSubview:titleLab];
        UILabel *titleLab2 = [[UILabel alloc] initWithFrame:SARectMake(90, 26, 200, 15)];
        titleLab2.text = self.listMode.productName;
        titleLab2.textColor = kLightgGrayColor;
        titleLab2.font = kFont(13);
        [_headerView addSubview:titleLab2];
        UIView *line = [[UIView alloc] initWithFrame:SARectMake(0, 50, kBaseScreenWidth, 1)];
        line.backgroundColor = kLineColor;
        [_headerView addSubview:line];
        UILabel *titleLab3 = [[UILabel alloc] initWithFrame:SARectMake(30, 60, 100, 15)];
        titleLab3.text = @"颜色";
        titleLab3.textColor = kLightgGrayColor;
        titleLab3.font = kFont(14);
        [_headerView addSubview:titleLab3];
        UILabel *titleLab4 = [[UILabel alloc] initWithFrame:SARectMake(248, 60, 60, 15)];
        titleLab4.text = @"颜色多选";
        titleLab4.textColor = kLightgGrayColor;
        titleLab4.font = kFont(14);
        [_headerView addSubview:titleLab4];
    }
    return _headerView;
}

-(UIButton *)switchBtn {
    if (!_switchBtn) {
        _switchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _switchBtn.frame = SARectMake(300, 43, 50, 50);
        [_switchBtn setImage:[UIImage imageNamed:@"shop_switch_n"] forState:UIControlStateNormal];
        [_switchBtn setImage:[UIImage imageNamed:@"shop_switch_s"] forState:UIControlStateSelected];
        [_switchBtn addTarget:self action:@selector(clickedSwitchBtn:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _switchBtn;
}

-(UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc]init];
        flow.scrollDirection = UICollectionViewScrollDirectionVertical;
        flow.itemSize = CGSizeMake(kScaleNum(60), kScaleNum(34));
        flow.minimumInteritemSpacing = kScaleNum(10);
        //    //设置每行之间的最小间隔
        flow.minimumLineSpacing = kScaleNum(10);
        
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(kScaleNum(30), kScaleNum(90), kScreenWidth - kScaleNum(60), kScaleNum(self.collectionViewHeight)) collectionViewLayout:flow];
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        [_collectionView registerClass:[SAInventoryCollectionViewCell class] forCellWithReuseIdentifier:@"SAInventoryCollectionViewCell"];
    }
    return _collectionView;
}

-(UIView *)footerView {
    if (!_footerView) {
        _footerView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight - kScaleNum(40 + SafeFooterHeight), kScreenWidth, kScaleNum(40 + SafeFooterHeight))];
        _footerView.backgroundColor = [UIColor whiteColor];
        UIButton *confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [confirmBtn setTitle:@"确认" forState:UIControlStateNormal];
        [confirmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        confirmBtn.frame = CGRectMake(kScreenWidth - kScaleNum(100), 0, kScaleNum(100), kScaleNum(40));
        confirmBtn.titleLabel.font = kFont(15);
        [confirmBtn setBackgroundImage:[UIImage imageNamed:@"top_image"] forState:UIControlStateNormal];
        [confirmBtn addTarget:self action:@selector(clickedConfirmBtn) forControlEvents:UIControlEventTouchUpInside];
        
        UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        [cancelBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        cancelBtn.frame = CGRectMake(kScreenWidth - kScaleNum(200), 0, kScaleNum(100), kScaleNum(40));
        cancelBtn.titleLabel.font = kFont(15);
        cancelBtn.backgroundColor = kColor(154, 154, 154, 1);
        [cancelBtn addTarget:self action:@selector(shareDissTapEvent) forControlEvents:UIControlEventTouchUpInside];
        self.cancelBtn = cancelBtn;
        cancelBtn.hidden = YES;
        
        UIButton *giveBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [giveBtn setTitle:@"赠送" forState:UIControlStateNormal];
        [giveBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        giveBtn.frame = CGRectMake(kScreenWidth - kScaleNum(300), 0, kScaleNum(100), kScaleNum(40));
        giveBtn.titleLabel.font = kFont(15);
        giveBtn.backgroundColor = kColor(130, 178, 154, 1);
        [giveBtn addTarget:self action:@selector(clickedGiveBtn) forControlEvents:UIControlEventTouchUpInside];
        self.giveBtn = giveBtn;
        giveBtn.hidden = YES;
        
        UIView *line = [[UIView alloc] initWithFrame:SARectMake(0, 0, kBaseScreenWidth, 1)];
        line.backgroundColor = kBgColor;
        [_footerView addSubview:line];
        [_footerView addSubview:confirmBtn];
        [_footerView addSubview:cancelBtn];
        [_footerView addSubview:giveBtn];
    }
    return _footerView;
}


-(UIImageView *)headerImageV {
    if (!_headerImageV) {
        _headerImageV = [[UIImageView alloc] initWithFrame:SARectMake(20, 136, 60, 70)];
        [_headerImageV sd_setImageWithURL:[NSURL URLWithString:self.listMode.img] placeholderImage:[UIImage imageNamed:@"shop_placeholderImg"]];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickedImageV)];
        _headerImageV.userInteractionEnabled = YES;
        _headerImageV.contentMode = UIViewContentModeScaleAspectFit;
        _headerImageV.clipsToBounds = YES;
        [_headerImageV setContentScaleFactor:[[UIScreen mainScreen] scale]];
        [_headerImageV addGestureRecognizer:tap];
    }
    return _headerImageV;
}

-(NSMutableArray<SASalesOrderColoursList *> *)selectColourMarr {
    if (!_selectColourMarr) {
        _selectColourMarr = [NSMutableArray array];
    }
    return _selectColourMarr;
}

-(UILabel *)inventoryLab {
    if (!_inventoryLab) {
        _inventoryLab = [[UILabel alloc] initWithFrame:SARectMake(120, 90 + self.collectionViewHeight, 62, 40)];
        _inventoryLab.font = kFont(15);
        _inventoryLab.textColor = [UIColor blackColor];
        _inventoryLab.text = @"当前库存";
        _inventoryLab.textAlignment = NSTextAlignmentCenter;
    }
    return _inventoryLab;
}

-(UILabel *)sizeLab {
    if (!_sizeLab) {
        _sizeLab = [[UILabel alloc] initWithFrame:SARectMake(30, 90 + self.collectionViewHeight, 50, 40)];
        _sizeLab.font = kFont(15);
        _sizeLab.textColor = [UIColor blackColor];
        _sizeLab.text = @"尺码";
        _sizeLab.textAlignment = NSTextAlignmentCenter;
    }
    return _sizeLab;
}

-(UIButton *)cutBtn {
    if (!_cutBtn) {
        _cutBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_cutBtn setImage:[UIImage imageNamed:@"shop_cutBtn"] forState:UIControlStateNormal];
        _cutBtn.frame = SARectMake(200, 90 + self.collectionViewHeight, 50, 40);
        _cutBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        [_cutBtn addTarget:self action:@selector(clickedCutBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cutBtn;
}

-(UIButton *)addBtn {
    if (!_addBtn) {
        _addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_addBtn setImage:[UIImage imageNamed:@"shop_addBtn"] forState:UIControlStateNormal];
        _addBtn.frame = SARectMake(308, 90 + self.collectionViewHeight, 50, 40);
        _addBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        [_addBtn addTarget:self action:@selector(clickedAddBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _addBtn;
}

-(UITextField *)numTf {
    if (!_numTf) {
        _numTf = [[UITextField alloc] initWithFrame:SARectMake(250, 90 + self.collectionViewHeight, 58, 40)];
        _numTf.delegate = self;
        _numTf.textAlignment = NSTextAlignmentCenter;
        _numTf.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
        [_numTf addTarget:self action:@selector(textFieldEditChanged:) forControlEvents:UIControlEventEditingChanged];
    }
    return _numTf;
}


@end
